var dbConfig = require('../utils/dbconfig')

//获取软件列表
getSoftwareList = (req, res) => {
    let { name, type, page, pageSize } = req.body
    let sql = 'select * from software ';
    if (name) {
        sql += ' where name like ?'
        if (type) {
            sql += `and type =${Number(type)}`
        }
    } else if (type) {
        sql += `where type = ${type}`
    } else {
        sql += 'order by id desc'
    }
    sql += ' limit ? offset ?'
    var sqlArr = name ? ["%" + name + "%", Number(pageSize), (Number(page) - 1) * Number(pageSize)] : [Number(pageSize), (Number(page) - 1) * Number(pageSize)];
    var callBack = (err, data) => {
        if (err) {
            console.log('连接出错', err)
        } else {
            let totalSql = 'select count(*) as total from software'
            dbConfig.SySqlConnect(totalSql, []).then(resutl => {
                res.send({
                    'list': data,
                    page: Number(page),
                    pageSize: Number(pageSize),
                    total: resutl[0]['total']
                })
            })

        }
    }
    dbConfig.sqlConnection(sql, sqlArr, callBack)
}
let addSoftwareList = async (req, res) => {
    let { name, logo, type, tips, detailImg, description, downloadUrl } = req.body
    let sql = 'insert into software(name,logo,type,tips,detailImg,description,downloadUrl) values(?,?,?,?,?,?,?)'
    let sqlArr = [name, logo, type, tips, detailImg, description, downloadUrl];
    let result = await dbConfig.SySqlConnect(sql, sqlArr)
    if (result && result.affectedRows) { //执行成功
        res.send({
            'success': true,
        })
    } else {
        res.send({
            'success': false,

        })
    }
}
let editSoftware = async (req, res) => {
    let { id, name, logo, type, tips, detailImg, description, downloadUrl } = req.body
    let sql = 'update software set name=?,logo=?,type=?,tips=?,detailImg=?,description=?,downloadUrl=? where id= ?'
    let sqlArr = [name, logo, type, tips, detailImg, description, downloadUrl, id];
    let result = await dbConfig.SySqlConnect(sql, sqlArr)
    if (result && result.affectedRows) { //执行成功
        res.send({
            'success': true,
        })
    } else {
        res.send({
            'success': false,

        })
    }
}
let deleteSoftware = async (req, res) => {
    let { id } = req.body
    let sql = 'delete from software where id =?'
    let sqlArr = [id]
    let result = await dbConfig.SySqlConnect(sql, sqlArr)
    if (result && result.affectedRows) { //执行成功
        res.send({
            'success': true,
        })
    } else {
        res.send({
            'success': false,

        })
    }
}

let getSoftwareDetail = async (req, res) => {
    let { id } = req.body
    let sql = 'select * from software where id =?'
    let sqlArr = [id]
    var callBack = (err, data) => {
        if (err) {
            console.log('连接出错', err)
        } else {
            res.send({
                'list': data,
            })

        }
    }
    dbConfig.sqlConnection(sql, sqlArr, callBack)
}
module.exports = {
    getSoftwareList, addSoftwareList, editSoftware, deleteSoftware, getSoftwareDetail
}